<?php

namespace app\home\controller;

//引入公共控制器
use app\common\Controller\Home;

class Teacher extends Home
{
    public $NoLogin = ['teacher_list', 'teacher_info'];

    protected $SubjectModel = null;
    protected $TeacherModel = null;
    protected $CategoryModel = null;
    protected $FollowModel = null;

    public function __construct()
    {
        //继承父类的构造函数
        parent::__construct();

        //加载模型
        $this->SubjectModel = model('common/Subject/Subject');
        $this->TeacherModel = model('Teacher.Teacher');
        $this->CategoryModel = model('Subject.Category');
        $this->FollowModel = model('Teacher.Follow');
    }

    // 名师列表
    public function Teacher_list()
    {
        if ($this->request->isAjax()) {
            $keywords = $this->request->param('keywords', '', 'trim');
            $page = $this->request->param('page', 1, 'trim');
            $limit = $this->request->param('limit', 10, 'trim');

            //偏移量
            $start = ($page - 1) * $limit;

            $where = [];

            if (!empty($keywords)) {
                $where['name|job'] = ['LIKE', "%$keywords%"];
            }

            //查询数据总条数
            $count = $this->TeacherModel->where($where)->count();

            //查询数据
            $list = $this->TeacherModel
                ->where($where)
                ->limit($start, $limit)
                ->select();

            $data = [
                'count' => $count,
                'list' => $list
            ];

            if ($list) {
                //成功返回
                $this->success('返回列表', null, $data);
                exit;
            } else {
                //失败返回
                $this->error('暂无数据', null, $data);
                exit;
            }
        }
        return $this->view->fetch();
    }

    // 名师详情
    public function Teacher_info()
    {
        $teacherid = $this->request->param('teacherid', 0, 'trim');

        //查询老师的信息
        $teacher = $this->TeacherModel->find($teacherid);

        if (!$teacher) {
            $this->error('老师信息不存在');
            exit;
        }

        //获取用户是否登录的信息
        $login = $this->IsLogin(false);

        if ($login) {
            $where = [
                'teacherid' => $teacherid,
                'busid' => $login['id']
            ];

            // 查询是否有关注过这个老师
            $check = $this->FollowModel->where($where)->find();

            // 增加自定义属性
            $teacher['follow'] = $check ? true : false;
        } else {
            $teacher['follow'] = false;
        }

        //判断是否有ajax请求
        if ($this->request->isAjax()) {
            $keywords = $this->request->param('keywords', '', 'trim');
            $page = $this->request->param('page', 1, 'trim');
            $limit = $this->request->param('limit', 10, 'trim');

            //偏移量
            $start = ($page - 1) * $limit;

            $where = ['teacherid' => $teacherid];

            if (!empty($keywords)) {
                $where['title'] = ['LIKE', "%$keywords%"];
            }

            //查询数据总条数
            $count = $this->SubjectModel->where($where)->count();

            //查询数据
            $list = $this->SubjectModel
                ->with(['category'])
                ->where($where)
                ->limit($start, $limit)
                ->select();

            $data = [
                'count' => $count,
                'list' => $list
            ];

            if ($list) {
                //成功返回
                $this->success('返回列表', null, $data);
                exit;
            } else {
                //失败返回
                $this->error('暂无数据', null, $data);
                exit;
            }
        }

        $this->assign([
            'teacher' => $teacher
        ]);

        return $this->view->fetch();
    }

    // 名师关注与取关
    public function follow()
    {
        $teacherid = $this->request->param('teacherid', 0, 'trim');

        //查询老师的信息
        $teacher = $this->TeacherModel->find($teacherid);

        if (!$teacher) {
            $this->error('老师信息不存在');
            exit;
        }

        //判断会员是否关注过名师
        $where = [
            'teacherid' => $teacherid,
            'busid' => $this->view->AutoLogin['id']
        ];

        $check = $this->FollowModel->where($where)->find();
        $result = $msg = '';

        //如果能找出记录就说明有关注过，取消关注 删除语句
        if ($check) {
            $result = $this->FollowModel->destroy($check['id']);
            $msg = '取关成功';
        } else {
            //没找到记录，没有关注过，关注 插入语句
            //组装数据
            $data = [
                'teacherid' => $teacherid,
                'busid' => $this->view->AutoLogin['id']
            ];

            $result = $this->FollowModel->validate('common/Teacher/Follow')->save($data);

            $msg = '关注成功';
        }

        if ($result === FALSE) {
            $this->error($this->FollowModel->getError());
            exit;
        } else {
            $this->success($msg);
            exit;
        }
    }
}
